<script lang="ts" setup>
import {ElMessage} from "element-plus";
import {onMounted, ref} from "vue";
import {reqCancelSale, reqRemoveSku, reqSaleSku, reqSkuInfo, reqSkuList} from "../../../api/product/sku";
import type {SkuData, SkuResponseData} from "../../../api/product/sku/type.ts";
import { ElCol, ElRow } from 'element-plus';

let currentPage = ref<number>(1); //当前页
let pageSize = ref<number>(3); //页码
let total = ref<number>(0); //总数
let skuParams = ref<SkuData[]>(); //数据存在这

//分页查询
const reqPage = async (page = 1) => {
  currentPage.value = page;
  const result: SkuResponseData = await reqSkuList(currentPage.value, pageSize.value as number);
  if (result.code === 200) {
    skuParams.value = result.data.records as SkuData[];
    total.value = result.data.total;
    currentPage.value = result.data.current;
    pageSize.value = result.data.size;
  }
}

//页面挂载后渲染
onMounted(() => {
  reqPage();
});

//抽屉的显示与否标识
let drawer = ref<boolean>(false);

//改变商品上下架
const updateSale = async (row: SkuData) => {
  if (row.isSale === 0) {
    await reqSaleSku(row.id as number);
    ElMessage({type: "success", message: "上架成功"});
    await reqPage(currentPage.value);
  } else {
    await reqCancelSale(row.id as number);
    ElMessage({type: "warning", message: "下架成功"});
    await reqPage(currentPage.value);
  }
}

const skuInfo = ref<SkuData>();//获取单个sku信息

//更新商品
const updateSku = (row: SkuData) => {
  ElMessage({type: "info", message: "可以复用spu里面的页面"});
  console.log(row);
}

//打开抽屉
const openDrawer = async (row: SkuData) => {
  let result = await reqSkuInfo(row.id as number);
  if (result.code === 200) {
    skuInfo.value = result.data;
    drawer.value = true;
  }
}

//删除商品
const removeSku = async (row: SkuData) => {
  let result = await reqRemoveSku(row.id as number);
  if (result.code === 200) {
    ElMessage({type: "success", message: "删除成功"});
    await reqPage(currentPage.value > 1 ? currentPage.value : currentPage.value - 1);
  } else {
    ElMessage({type: "error", message: "删除失败"});
  }
}
</script>

<template>
  <el-card>
    <el-table :data="skuParams" border style="margin: 10px 0">
      <el-table-column align="center" label="序号" type="index" width="80px"></el-table-column>
      <el-table-column label="名称" show-overflow-tooltip prop="skuName"></el-table-column>
      <el-table-column label="概述" show-overflow-tooltip prop="skuDesc"></el-table-column>
      <el-table-column label="默认图片">
        <template #default="{row}">
          <img :src="row.skuDefaultImg" alt="" style="text-align: center" width="100px">
        </template>
      </el-table-column>
      <el-table-column label="重量(g)" prop="weight"></el-table-column>
      <el-table-column label="价格(元)" prop="price"></el-table-column>
      <el-table-column label="操作">
        <template #default="{row}">
          <el-button :icon="row.isSale === 1 ? 'Upload' : 'Bottom'" size="small" type="success" @click="updateSale(row)"></el-button>
          <el-button icon="Edit" size="small" type="primary" @click="updateSku(row)"></el-button>
          <el-button icon="InfoFilled" size="small" type="info" @click="openDrawer(row)"></el-button>
          <el-button icon="DeleteFilled" size="small" type="danger" @click="removeSku(row)"></el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
        v-model:current-page="currentPage"
        v-model:page-size="pageSize"
        :page-sizes="[3, 5, 7, 9]"
        :total="total"
        background
        layout="prev, pager, next, jumper, -> ,total, sizes,"
        @size-change="reqPage(1)"
        @current-change="reqPage(currentPage)"
    ></el-pagination>
    <el-drawer v-model="drawer" title="商品详情">
      <el-row style="margin: 10px 0">
        <el-col :span="8">名称</el-col>
        <el-col :span="16">{{ skuInfo?.skuName }}</el-col>
      </el-row>
      <el-row style="margin: 10px 0">
        <el-col :span="8">描述</el-col>
        <el-col :span="16">{{ skuInfo?.skuDesc }}</el-col>
      </el-row>
      <el-row style="margin: 10px 0">
        <el-col :span="8">价格</el-col>
        <el-col :span="16">{{ skuInfo?.price }}</el-col>
      </el-row>
      <el-row style="margin: 10px 0">
        <el-col :span="8">平台属性</el-col>
        <el-col :span="16">
          <el-tag style="margin-right: 10px" v-for="item in skuInfo?.skuAttrValueList" :key="item.attrValueId" size="small" type="warning">
            {{ item.attrValueName }}</el-tag>
        </el-col>
      </el-row>
      <el-row style="margin: 10px 0">
        <el-col :span="8">销售属性</el-col>
        <el-col :span="16">
          <el-tag style="margin-right: 10px" v-for="item in skuInfo?.skuSaleAttrValueList" :key="item.saleAttrValueId" size="small" type="danger">
            {{ item.saleAttrValueName }}
          </el-tag>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="8">商品图片</el-col>
        <el-col :span="16">
          <el-carousel :interval="4000" height="200px" type="card">
            <el-carousel-item v-for="item in skuInfo.skuImageList" :key="item">
              <img :src="item.imgUrl" alt="" style="width: 100px">
            </el-carousel-item>
          </el-carousel>
        </el-col>
      </el-row>
    </el-drawer>

  </el-card>
</template>

<style lang="scss" scoped>
.el-carousel__item h3 {
  color: #475669;
  opacity: 0.75;
  line-height: 200px;
  margin: 0;
  text-align: center;
}

.el-carousel__item:nth-child(2n) {
  background-color: #99a9bf;
}

.el-carousel__item:nth-child(2n + 1) {
  background-color: #d3dce6;
}
</style>